<?php

/**
 *
 * 
 * モデルとデータベーステーブルの作成
 * http://framework.zend.com/manual/1.12/ja/learning.quickstart.create-model.html
 * ユーザー認証
 * http://japan.internet.com/developer/20100126/26.html
 * databaseでの認証
 * http://framework.zend.com/manual/1.12/ja/zend.auth.adapter.dbtable.html
 * formの作成
 * http://framework.zend.com/manual/1.11/en/learning.quickstart.create-form.html
 * 
 */
require_once 'BaseController.php';
require_once 'Zend/Loader.php';

class IndexController extends BaseController
{
    public $userAgentManager;

    public $secureManager;

    public function init()
    {
        session_start();
        header("Access-Control-Allow-Origin: *");

        Zend_Loader::loadFile('UserAgentManager.class.php', APPLICATION_PATH . "/Library");
        Zend_Loader::loadFile('SecureManager.class.php', APPLICATION_PATH . "/Library");
        Zend_Loader::loadFile('FeedUtil.class.php', APPLICATION_PATH . "/Library");

        $this->userAgentManager = new UserAgentManager();
        $this->secureManager = new SecureManager();
        $this->feedUtil = new feedUtil();
    }

    public function indexAction()
    {

        $frontController = Zend_Controller_Front::getInstance();
        $appConfig = $frontController->getParam('bootstrap')->getOptions();

        $this->view->secureManager = $this->secureManager;
        $this->view->feedUtil = $this->feedUtil;
        $this->view->is_smartphone = $this->userAgentManager->is_smartphone();


        $feed_url = "";
        $feed_url_param = "";
        $feed_url_session = "";
        $feed_share_url = "";
        $playlistItems = array();
        $grid_tag = "";
        $nextPageToken = "";
        $prevPageToken = "";
        $totalResults = "";
        $OAUTH2_CLIENT_ID = $appConfig['clientId'];
        $OAUTH2_CLIENT_SECRET = $appConfig['clientSecret'];
        $developerKey = $appConfig['developerKey'];
        $applicationName = $appConfig['applicationName'];
        $default_keyword = "jazz";
        $keyword = "";
        if (isset($_REQUEST['keyword']))
        {
            $keyword = $_REQUEST['keyword'];
        }

        if (isset($_REQUEST['feed_url']))
        {
            $feed_url_param = $this->secureManager->sanitize($_REQUEST['feed_url']);
        }else if (isset($_SESSION['feed_url']))
        {
            $feed_url_session = $this->secureManager->sanitize($_SESSION['feed_url']);
        }
        

        if (!empty($feed_url_param))
        {
            $feed_url = $feed_url_param;
            $feed_share_url = 'http://' . $_SERVER['SERVER_NAME'] . '?feed_url=' . urlencode($feed_url);
        }
        elseif (!empty($feed_url_session))
        {
            $feed_url = $feed_url_session;
            $feed_share_url = 'http://' . $_SERVER['SERVER_NAME'] . '?feed_url=' . urlencode($feed_url);
        }
        if (empty($feed_url) && empty($keyword))
        {
            $keyword = $default_keyword;
        }

        $feed_share_url = 'http://' . $_SERVER['SERVER_NAME'] . '?feed_url=' . urlencode($feed_url);

        try
        {
            if (!empty($keyword))
            {
                $playlistItems = $this->feedUtil->feedsFromKeyword($keyword, $OAUTH2_CLIENT_ID, $OAUTH2_CLIENT_SECRET, $developerKey);
            }
            else if (!empty($feed_url))
            {
                $playlistItems = $this->feedUtil->feedsFromPlaylistOthers($feed_url, $OAUTH2_CLIENT_ID, $OAUTH2_CLIENT_SECRET, $developerKey);
            }
            $grid_tag = $this->feedUtil->feedToHtml($playlistItems);
            $default_video_id = $this->feedUtil->getRandomVideoId($playlistItems);

            if (isset($playlistItems['pageInfo']))
            {
                $totalResults = $playlistItems['pageInfo']['totalResults'];
            }
            if (isset($playlistItems['nextPageToken']))
            {
                $nextPageToken = $playlistItems['nextPageToken'];
            }
            if (isset($playlistItems['prevPageToken']))
            {
                $prevPageToken = $playlistItems['prevPageToken'];
            }

            $this->view->default_video_id = $default_video_id;
            $this->view->keyword = $keyword;
            $this->view->grid_tag = $grid_tag;
            $this->view->feed_share_url = $feed_share_url;
            $this->view->developerKey = $developerKey;
            $this->view->applicationName = $applicationName;
            
        }catch (Exception $exc)
        {
            echo "Can't Load playlist.";
        }
    }

    public function registerAction()
    {
        $this->view->errors = array();
        if ($this->getRequest()->isPost())
        {
            $usersMapper = new Application_Model_UsersMapper();
            $users = new Application_Model_Users();
            $users->setEmail($this->_request->getParam("email"));
            $users->setPaswd($this->_request->getParam("paswd"));
            $result = $usersMapper->save($users);
            if (!is_array($result))
            {
                $this->view->created = 1;
            }
            else
            {
                $this->view->errors = $result;
                $this->view->email = $this->_request->getPost('email');
                $this->view->pswd = $this->_request->getPost('pswd');
            }
        }
    }

    public function oauthAction()
    {
        
    }

    public function operationsAction()
    {
        
    }

    public function logoutAction()
    {

        if (isset($_SESSION['token']))
        {
            unset($_SESSION['token']);
        }
        if (isset($_SESSION['sessionToken']))
        {
            unset($_SESSION['sessionToken']);
        }
        if (isset($_SESSION['feed_url']))
        {
            unset($_SESSION['feed_url']);
        }
        echo $this->view->render('index/login.phtml');
    }

    public function loginAction()
    {
        $this->logoutAction();
        exit;
    }

    public function downloadAction()
    {
        
    }

}

